<?php
require_once(dirname(__FILE__)."/../../libs/dbclass/dbconnect.php");
require_once(dirname(__FILE__)."/../../libs/string/libstr.php");

$mc= new libstr();
class power{
	
	//删除权限组类别
function cata_del($rootid,$tb=""){
	global $mc,$db;
	$_tb="powercontent_class";
	if($mc->Ismp($tb)!=""){
	$_tb=$tb;	
	}
	if($mc->Ismp($rootid)!=""){
$myrs4=$db->dvstr("select `lock` from ".$_tb." where rootid ='".$rootid."' ");

	if(trim($myrs4["lock"])=="1")
	{
		//return "cata is lock";限制删除
	}
	
		$db->cmd("delete from ".$_tb." where rootid like '%".$rootid."%'");//连子集一起删除
		return true;
	}
	return false;
}
//添加权限类别
//$url针对后台菜单
function cata_add($classname,$pid=0,$content="",$order=0,$tb="",$quanxian=0,$url=""){
		global $mc,$db;
		if( $mc->Ismp($classname)==""){
			return false;
		}
	$_tb="powercontent_class";
	if($mc->Ismp($tb)!=""){
	$_tb=$tb;	
	}
	$rootid="0,";
	if($pid!=0){
	$rs1=$db->ds("select * from ".$_tb." where ID=".trim($pid)." order by ID asc ");
	if(count($rs1)>0){
		$rootid=trim($rs1[0]["rootid"]);
	}
	}
	$sqlstr="insert into ".$_tb."(classname,classorder)values('".trim($classname)."','".$order."')";
	$db->cmd($sqlstr);
	$newID = $db->insert_id();
    $rootid.=$newID.",";
	$depthid = 0;
	$mydepthid = split(",",$rootid);
	if (count($mydepthid) >= 0)
	{
		$depthid = count($mydepthid) - 3;
	}
	$sql="update ".$_tb." set rootid='".$rootid."',pid=".$pid.",content='".$content."',depthid=".$depthid."  where ID=".$newID."";	
	if($tb=="member_class"){
	$sql="update ".$_tb." set rootid='".$rootid."',pid=".$pid.",content='".$content."',depthid=".$depthid.",quanxian=".$quanxian." where ID=".$newID."";	
	}
	if($tb=="menu_class"){
	$sql="update ".$_tb." set rootid='".$rootid."',pid=".$pid.",content='".$content."',depthid=".$depthid.",url='".$url."' where ID=".$newID."";	
	}
$db->cmd($sql);
return true;	
	}
	
//判断子集
	function myck($id,$tb="")
{
	global $db;
	global $mc;
	$_tb="powercontent_class";
	if($mc->Ismp($tb)!=""){
	$_tb=$tb;	
	}
	$rs1=$db->ds("select * from ".$_tb." where pid='".trim($id)."'");
	if(count($rs1)>0){
		return true;
	}else{
		return false;
	}
}

//修改类别
function cata_edit($id,$classname,$pid=0,$content="",$order=0,$tb="",$qx=0,$ul=""){
	global $mc,$db;
    $_tb="powercontent_class";
    $quanxian="";//配合member_class表使用
     $url="";//配合menu_class表使用
	if($mc->Ismp($tb)!=""){
	$_tb=$tb;	
	}
	if($_tb=="member_class"){
		$quanxian=" ,quanxian=".$qx;
	}
	if($_tb=="menu_class"){
		$url=" ,url='".$ul."'";
	}
		if(  $mc->Ismp($id)=="" || $mc->Ismp($classname)==""){
			return false;
		}
		if($id==$pid){
			$sql="update ".$_tb." set content='".$content."',classname='".$classname."',classorder='".$classorder."' ".$quanxian." ".$url." where ID=".trim($id)."";
		}else{
		if($pid!=0){
	$rs1=$db->ds("select * from ".$_tb." where ID=".trim($pid)." order by ID asc ");
	if(count($rs1)>0){
		$rootid=trim($rs1[0]["rootid"]);
		$rootid.=$id.",";
	}
	}else{
		$rootid="0,".$id.",";
	}	
	$depthid = 0;
	$mydepthid = split(",",$rootid);
	if (count($mydepthid) >= 0)
	{
		$depthid = count($mydepthid) - 3;
	}
	if($this->myck($id)=='true'){
	$sql="update ".$_tb." set content='".$content."',classname='".$classname."',classorder='".$classorder."'".$quanxian." ".$url." where ID=".trim($id)."";
	}else{
		$sql="update ".$_tb." set content='".$content."' ,classname='".$classname."',classorder='".$order."'".$quanxian." ".$url.",pid=".$pid." ,rootid='".$rootid."',depthid=".$depthid."  where ID=".$id."";
	}
		}
		
	$db->cmd($sql);
	return true;
}


//获得类别详细
function cata_detail($id,$tb=""){
	global $mc,$db;
	$_tb="powercontent_class";
	if($mc->Ismp($tb)!=""){
	$_tb=$tb;	
	}
	if(  $mc->Ismp($id)==""){
			return false;
		}
	$rs1=$db->dvstr("select * from ".$_tb." where ID=".trim($id)." order by ID asc limit 1 ");
	return 	$rs1;

}



}

?>